package top.gohigh.sxdial3;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.fupan.sxdial3.R;

/**
 * Created by fupan on 2017/12/25.
 */

public class QuickClearEditText extends android.support.v7.widget.AppCompatEditText {
    private final static String TAG = "QuickClearEditText";

    private Drawable mClearDrawable;
    private Context mContext;

    private boolean hasFocus=false;

    public QuickClearEditText(Context context){
        super(context);
        mContext=context;
        initDrawable();

    }

    public QuickClearEditText(Context context, AttributeSet attrs){
        super(context, attrs);
        mContext=context;
        initDrawable();


    }

    public QuickClearEditText(Context context,AttributeSet attrs, int defStyleAttr){
        super(context,attrs,defStyleAttr);
        mContext =context;
        initDrawable();
    }

    private  void initDrawable(){

        mClearDrawable = mContext.getResources().getDrawable(R.drawable.ic_cancel_black_24dp);

        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                setDrawable();
            }
        });

        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hasFocus = b;
                setDrawable();
            }
        });


        setDrawable();

    }


    private void  setDrawable(){
        if(hasFocus&&length()>=1){
            setCompoundDrawablesWithIntrinsicBounds(null,null,mClearDrawable,null);

        }else{
            setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);

        }

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {


        if(event.getAction() == MotionEvent.ACTION_UP &&mClearDrawable!=null){
            int eventX = (int) event.getX();
            int eventY= (int) event.getY();
           // Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);




            int left = getWidth() - getPaddingRight() - mClearDrawable.getIntrinsicWidth();
            int right = getWidth() - getPaddingRight();

            int top = (getHeight() - mClearDrawable.getIntrinsicHeight())/2;
            int bottom = getHeight() - top;


            if(eventY>top &&eventY<bottom&&eventX>left&&eventX<right){
                setText("");
            }



        }
        return super.onTouchEvent(event);
    }

}
